package com.google.code.sms.domain;

import java.io.File;
import java.util.Date;
import com.google.code.sms.service.metadata.MetaData;

import org.apache.commons.io.FilenameUtils;

/**
 * A media element (audio, video, directory).
 *
 * @author Scott Ware
 */
public class MediaElement {

    private int id;
    private MediaElementType elementType;
    private String name;
    private String path;
    private String folder;
    private String parentPath;
    private String parentName;
    private int playCount;
    private Date lastPlayed;
    private Date created;
    private Date changed;
    private Date lastScanned;
    private Date childrenLastUpdated;
    private boolean present;
    private String format;
    private Long fileSize;
    
    private MetaData metaData;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
    
    public MediaElement.MediaElementType getMediaElementType() {
        return elementType;
    }

    public void setMediaElementType(MediaElement.MediaElementType elementType) {
        this.elementType = elementType;
    }
    
    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }
    
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getFolder() {
        return folder;
    }

    public void setFolder(String folder) {
        this.folder = folder;
    }

    public File getFile() {
        return new File(path);
    }
    
    public String getElementName() {
        return FilenameUtils.getBaseName(path);
    }

    public boolean exists() {
        return getFile().exists();
    }
    
    public String getParentPath() {
        return parentPath;
    }

    public void setParentPath(String parentPath) {
        this.parentPath = parentPath;
    }

    public String getParentName() {
        return parentName;
    }

    public void setParentName(String parentName) {
        this.parentName = parentName;
    }
    
    public File getParentFile() {
        return getFile().getParentFile();
    }

    public int getPlayCount() {
        return playCount;
    }

    public void setPlayCount(int playCount) {
        this.playCount = playCount;
    }

    public Date getLastPlayed() {
        return lastPlayed;
    }

    public void setLastPlayed(Date lastPlayed) {
        this.lastPlayed = lastPlayed;
    }

    public Date getCreated() {
        return created;
    }

    public void setCreated(Date created) {
        this.created = created;
    }

    public Date getChanged() {
        return changed;
    }

    public void setChanged(Date changed) {
        this.changed = changed;
    }

    public Date getLastScanned() {
        return lastScanned;
    }

    public void setLastScanned(Date lastScanned) {
        this.lastScanned = lastScanned;
    }

    /**
     * Returns when the children was last updated in the database.
     */
    public Date getChildrenLastUpdated() {
        return childrenLastUpdated;
    }

    public void setChildrenLastUpdated(Date childrenLastUpdated) {
        this.childrenLastUpdated = childrenLastUpdated;
    }

    public boolean isPresent() {
        return present;
    }

    public void setPresent(boolean present) {
        this.present = present;
    }
    
    public String getFormat() {
        return format;
    }

    public void setFormat(String format) {
        this.format = format;
    }
    
    public Long getFileSize() {
        return fileSize;
    }

    public void setFileSize(Long fileSize) {
        this.fileSize = fileSize;
    }
    
    public MetaData getMetaData()
    {
        return metaData;
    }
    
    public void setMetaData(MetaData metaData)
    {
        this.metaData = metaData;
    }
    
    public boolean isAudio()
    {
        return elementType == MediaElementType.AUDIO;
    }
    
    public boolean isVideo()
    {
        return elementType == MediaElementType.VIDEO;
    }
    
    public boolean isDirectory()
    {
        return elementType == MediaElementType.DIRECTORY || elementType == MediaElementType.MEDIA_DIRECTORY;
    }
    
    public boolean isRootDirectory()
    {
        return elementType == MediaElementType.DIRECTORY;
    }
    
    public boolean isMediaDirectory()
    {
        return elementType == MediaElementType.MEDIA_DIRECTORY;
    }
    
    @Override
    public boolean equals(Object o) {
        return o instanceof MediaElement && ((MediaElement) o).path.equals(path);
    }

    @Override
    public int hashCode() {
        return path.hashCode();
    }
    
    public static enum MediaElementType {
        AUDIO,
        VIDEO,
        DIRECTORY,
        MEDIA_DIRECTORY
    }
}